package com.suxii.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.suxii.netty_demo.R;
import com.suxii.netty_demo.activity.ChatActivity;
import com.suxii.netty_demo.fragment.MessageFragment;
import com.suxii.utils.Global;

import java.util.ArrayList;
import java.util.List;

public class FriendListAdapter extends RecyclerView.Adapter<FriendListAdapter.FriendViewHolder> {
    private List<String> data;
    private Context context;

    private List<String> name;
    private List<Integer> uuid;

    public FriendListAdapter(List<String> data, Context context) {
        this.data = data;
        this.context = context;
        init();
    }
    private void init(){
        name=new ArrayList<>();
        uuid=new ArrayList<>();
        data.forEach(s->{
            String[] d = s.split(" ");
            name.add(d[0]);
            uuid.add(Integer.valueOf(d[1]));
        });
    }

    @NonNull
    @Override
    public FriendViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_friend, parent, false);
        return new FriendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FriendViewHolder holder, int position) {
        holder.friendName.setText(name.get(position));
        holder.itemView.setOnLongClickListener(view -> itemLongListener.onItemLongListener(view,name.get(position),uuid.get(position)));
        holder.itemView.setOnClickListener(view -> {
            Intent intent=new Intent(context, ChatActivity.class);
            Global.uuid= uuid.get(position);
            context.startActivity(intent);

        });
    }

    // 回调事件
    public interface ItemLongListener{
        boolean onItemLongListener(View view,String name,int uid);
    }
    private ItemLongListener itemLongListener;
    public void setOnItemLongListener(ItemLongListener itemLongListener){
        this.itemLongListener=itemLongListener;
    }


    @Override
    public int getItemCount() {
        return uuid.size();
    }

    class FriendViewHolder extends RecyclerView.ViewHolder {

        private TextView friendName;

        public FriendViewHolder(@NonNull View itemView) {
            super(itemView);
            friendName=itemView.findViewById(R.id.friend_name);
        }
    }
}
